import express, { Application, Request, Response, NextFunction } from "express";
import users from "./router/users";
import cors from "cors";
import logger from "morgan";
import createHttpError from "http-errors";
const app: Application = express();

app.get("/", (req, res) => {
  res.send("Hello");
});

app.use(logger("dev"));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/users", users);

app.use(function (req, res, next) {
  next(createHttpError(404));
});

// error handler
app.use(function (err: any, req: Request, res: Response, next: NextFunction) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  res.status(err.status || 500);
  res.json({ message: err.message, code: err.code || 20004, path: req.path });
  next(err);
});

const port: number = 5000;
app.listen(port, () => {
  console.log(`server is running on ${port}`);
});
